import { CardCategoryList, TriggerType, TriggerTypeList, UaCard } from "../../models/ua-card.model";
import { GlobalService } from "../../services/global.service";
import { UserDeckService } from "../../services/ua-user-deck.service";

// components/deck-preview/deck-preview.component.ts
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    deckId: {
      type: Number,
      value: 0
    },
    ipId: {
      type: Number,
      value: 0
    },
  },

  observers: {
    'ipId': function () {
      if (!!this.data.ipId && !!this.data.deckId) {
        this.load();
      }
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    STATIC_ASSETS_HOST: GlobalService.STATIC_ASSETS_HOST,
    FILE_HOST: GlobalService.FILE_HOST,
    cardList: [] as UaCard[],
    idCountMap: {} as { [key: number]: number },
    energyCountMap: {} as { [key: string]: number },
    energyTypeKeys: [] as string[],
    categoryCountMap: {} as { [key: string]: number },
    categoryTypeKeys: [] as string[],
    triggerCountMap: {} as { [key: string]: number },
    triggerTypeKeys: [] as string[],
    total: 0,
    totalTrigger: 0,
    colorCount: 0,
    finalCount: 0,
    specialCount: 0,
    detailVisible: false,
    detailCard: {} as UaCard
  },


  /**
   * 组件的方法列表
   */
  methods: {
    load() {
      const categoryCountMap: { [key: string]: number } = {};
      const categoryTypeKeys: string[] = [];
      for (const item of CardCategoryList) {
        categoryTypeKeys.push(item.value.toString().toLowerCase());
        categoryCountMap[item.value.toString().toLowerCase()] = 0;
      }
      const triggerCountMap: { [key: string]: number } = {};
      const triggerTypeKeys: string[] = [];
      for (const item of TriggerTypeList) {
        triggerTypeKeys.push(item.value.toString().toLowerCase());
        triggerCountMap[item.value.toString().toLowerCase()] = 0;
      }
      UserDeckService.findById(this.data.deckId!).then((deckDto) => {
        if (deckDto) {
          const cardMap: { [key: number]: UaCard } = {}
          const cardList = deckDto.cardDtoList ?? [];
          cardList.map((card: UaCard) => {
            cardMap[card.id!] = card;
          })
          const idCountMap: { [key: number]: number } = {};
          const cardDeckNoCountMap: { [key: string]: number } = {};
          for (const item of deckDto.cardList ?? []) {
            idCountMap[item.id!] = item.count!;
            const card = cardMap[item.id!];
            if (card) {
              if (!cardDeckNoCountMap[card.cardDeckNo!]) {
                cardDeckNoCountMap[card.cardDeckNo!] = 0;
              }
              cardDeckNoCountMap[card.cardDeckNo!] += item.count!;
            }
          }

          const deckNos: string[] = [];
          const deckCardList: UaCard[] = [];
          const triggerCountMap: { [key: string]: number } = {};
          const categoryCountMap: { [key: string]: number } = {};
          const energyCountMap: { [key: string]: number } = {};
          const energyTypeKeys: string[] = [];
          const energyCountOrderMap: { [key: string]: number } = {};
          let total = 0;
          let totalTrigger = 0;
          for (const card of cardList) {
            card.cardImgSm = card.cardImg?.replace('.png', '.jpg');
            if (!!cardDeckNoCountMap[card.cardDeckNo!] && !deckNos.includes(card.cardDeckNo!)) {
              deckNos.push(card.cardDeckNo!);
              const c = cardDeckNoCountMap[card.cardDeckNo!];
              if (card.triggerType) {
                if (!triggerCountMap[card.triggerType.toString().toLowerCase()]) {
                  triggerCountMap[card.triggerType.toString().toLowerCase()] = 0;
                }
                triggerCountMap[card.triggerType.toString().toLowerCase()] += c;
                totalTrigger += c;
              }
            }
            if (!!idCountMap[card.id!]) {
              deckCardList.push(card);
              if (card.cardCategory) {
                if (!categoryCountMap[card.cardCategory.toString().toLowerCase()]) {
                  categoryCountMap[card.cardCategory.toString().toLowerCase()] = 0;
                }
                categoryCountMap[card.cardCategory.toString().toLowerCase()] += idCountMap[card.id!];
                total += idCountMap[card.id!];
              }
              const ek = `${card.needEnergyType!.toLowerCase() + card.needEnergyCount!}`;
              if (!energyCountMap[ek]) {
                energyCountMap[ek] = 0;
                energyCountOrderMap[ek] = card.needEnergyCount!;
              }
              if (!energyTypeKeys.includes(ek)) {
                energyTypeKeys.push(ek)
              }
              energyCountMap[ek] += idCountMap[card.id!];
            }
          }
          energyTypeKeys.sort((a, b) => energyCountOrderMap[a] - energyCountOrderMap[b]);
          const finalCount = !!triggerCountMap[TriggerType.Final.toLowerCase()] ? triggerCountMap[TriggerType.Final.toLowerCase()] : 0;
          const colorCount = !!triggerCountMap[TriggerType.Color.toLowerCase()] ? triggerCountMap[TriggerType.Final.toLowerCase()] : 0;
          const specialCount = !!triggerCountMap[TriggerType.Special.toLowerCase()] ? triggerCountMap[TriggerType.Final.toLowerCase()] : 0;
          this.setData({
            idCountMap,
            categoryCountMap,
            triggerCountMap,
            total,
            totalTrigger,
            triggerTypeKeys,
            energyCountMap,
            energyTypeKeys,
            finalCount,
            colorCount,
            specialCount,
            cardList: deckCardList
          });
        }
      });

    },

    openDetail(e: any) {
      console.log(e)
      for (const card of this.data.cardList) {
        if (card.id === e.currentTarget.dataset['id']) {
          this.setData({
            detailVisible: true,
            detailCard: card
          });
          break;
        }
      }
    },

    onDetailVisibleChange(e: any) {
      this.setData({
        detailVisible: e.detail.visible,
      });
    },

    onPrev(e: any) {
      const cardList = this.data.cardList;
      const detailCardId = e.detail;
      let p = 0;
      for (let i = 0; i < cardList.length; i++) {
        if (detailCardId === cardList[i].id) {
          p = i;
          break;
        }
      }
      if (p === 0) {
        p = cardList.length - 1;
      } else {
        p--;
      }
      this.setData({
        detailCard: cardList[p]
      })
    },

    onNext(e: any) {
      const cardList = this.data.cardList;
      const detailCardId = e.detail;
      let p = 0;
      for (let i = 0; i < cardList.length; i++) {
        if (detailCardId === cardList[i].id) {
          p = i;
          break;
        }
      }
      if (p === cardList.length - 1) {
        p = 0;
      } else {
        p++;
      }
      this.setData({
        detailCard: cardList[p]
      })
    }
  },
})